
#region copyright notice
/* ====================================================================
 * The UncleGED Open Source Software License
 *
 * Copyright (c) 1999-2009 D. M. DeBacker. All rights reserved.
 * http://gatheringleaves.org/uged
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Products derived from this software may not be called "UncleGED",
 *    nor may "UncleGED" appear in their name, without prior written
 *    permission of D. M. DeBacker.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL D. M. DEBACKER BE LIABLE FOR ANY 
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 */
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using log4net;
using System.Reflection;

using UncleGED.Utils;
using UncleGED.Application;

namespace UGed
{
   public partial class MainForm : Form
   {
      // These are the Win32 error code for file not found or access denied.
      const int ERROR_FILE_NOT_FOUND = 2;
      const int ERROR_ACCESS_DENIED = 5;

      UGedProject m_UGProj = new UGedProject();
      LocalApplication m_localSettings = new LocalApplication();
      string m_ProjectFile = "";
      private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		
      public UGedProject Project
      {
         get { return m_UGProj; }
      }

      bool m_DidSave = false;
      public bool DidSave
      {
         get { return m_DidSave; }
      }

      public MainForm()
      {
         InitializeComponent();
			string path = FolderMethods.GetUserPath();

         m_localSettings = ConfigReader.LoadSettings(path + "\\projects.cfg");

         if (m_localSettings.MostRecentProject != "")
         {
            m_ProjectFile = m_localSettings.MostRecentProject;
         }
         else
         {            
            m_ProjectFile = path + "\\_default.ugproj";
         }
      }


      public MainForm(string projectFile)
      {
         InitializeComponent();
			string path = FolderMethods.GetUserPath();

         m_localSettings = ConfigReader.LoadSettings(path + "\\projects.cfg");

         m_ProjectFile = projectFile;

		}

      private void MainForm_Load(object sender, EventArgs e)
      {
         try
         {
            string userPath = FolderMethods.GetUserPath();
            DirectoryInfo dinfo = new DirectoryInfo(userPath);

            if (dinfo.Exists == false)
            {
               dinfo = Directory.CreateDirectory(userPath);
               dinfo.CreateSubdirectory("gedcom");
               dinfo.CreateSubdirectory("html");
            }
         }
         catch
         {
         }


         //start the logger	
         string sPath = FolderMethods.GetAppPath();

         //start logging
			string log4netFile = sPath + "\\log4net.config";
			System.IO.FileInfo configFile = new System.IO.FileInfo(log4netFile);
			log4net.Config.XmlConfigurator.ConfigureAndWatch(configFile);
			  
			_logger.Info("Starting UncleGED - Ver. " + Assembly.GetExecutingAssembly().GetName().Version.ToString()); //System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion);

         m_UGProj = new UGedProject();
         m_UGProj = ConfigReader.LoadProject(m_ProjectFile);

         DisplaySettings();
			tmrStartup.Start();
      }


      private void DisplaySettings()
      {
         this.txtProjectName.Text = m_UGProj.ProjectName;
         this.txtGEDCOMFile.Text = m_UGProj.GedComFile;
         this.txtGEDCOMPath.Text = m_UGProj.GedcomPath;
         this.txtProjectName.Text = m_UGProj.ProjectName;
         this.txtHtmlPath.Text = m_UGProj.HtmlPath;

         this.chkPrivate.Checked = m_UGProj.Privacy;
         this.chkBackground.Checked = m_UGProj.UseBackground;
         this.chkBanner.Checked = m_UGProj.UseBanner;

			this.chkAddPedigreeToPerson.Checked = m_UGProj.AddPedigreeToPerson;

         //this.chkDoCompileHtml.Checked = m_UGProj.DoCompileHTML;

         this.txtBackgroundImage.Text = m_UGProj.BackgroundFile;
         this.txtBannerImage.Text = m_UGProj.BannerFile;

         this.chkExcludeNotes.Checked = m_UGProj.ExcludeNotes;
         this.chkSourceInNewWindow.Checked = m_UGProj.SourcesInNewWindow;

         this.chkAddKeywords.Checked = m_UGProj.AddKeywords;
         this.chkSurnameCount.Checked = m_UGProj.AddSurnameCount;
         this.chkCreateListReport.Checked = m_UGProj.CreateListReport;
         this.chkCreateStatsPage.Checked = m_UGProj.CreateStatsPage;
         this.chkDisplayRIN.Checked = m_UGProj.DisplayRIN;
         this.chkDisplayMRIN.Checked = m_UGProj.DisplayMRIN;

         this.chkAddCopyright.Checked = m_UGProj.AddUserCopyright;
         this.chkCreateGendexFile.Checked = m_UGProj.CreateGendexFile;

         //this.chkWarnNonAnsi.Checked = m_UGProj.WarnNonAnsi;

         this.txtAuthorName.Text = m_UGProj.AuthorsName;
         this.txtEmail.Text = m_UGProj.AuthorsEmail;
         this.txtAuthorsURL.Text = m_UGProj.AuthorsUrl;
			this.chkHomeEqualsHomePage.Checked = m_UGProj.HomeEqualsHomePage;

         this.chkCreateChronoPage.Checked = m_UGProj.CreateChronoPage;
         this.nudChronoCutoff.Value = m_UGProj.ChronoCutOff;

         this.txtFamFilePrefix.Text = m_UGProj.FamFilePrefix;

         this.chkSurnamesAllCaps.Checked = m_UGProj.SurnamesAllCaps;
         this.chkChronoLinks.Checked = m_UGProj.ChronoLinks;

         this.chkMostRecentOnPage.Checked = m_UGProj.MostRecentOnPage;
         this.chkMostRecentOnIndex.Checked = m_UGProj.MostRecentOnIndex;
			this.chkSuppressLastUpdateOnHomePage.Checked = m_UGProj.SuppressLastUpdateOnHomePage;
         this.chkHighlight.Checked = m_UGProj.HiLiteMostRecent;

         this.chkCreateLocationsPage.Checked = m_UGProj.CreateLocPage;
         this.chkLocReverseOrder.Checked = m_UGProj.LocReverseOrder;
         this.chkLocDisplayEvents.Checked = m_UGProj.LocDisplayEventDates;

         if(m_UGProj.LocEstimateMethod == 0) this._optLocEstimated_0.Checked = true;
         if(m_UGProj.LocEstimateMethod == 1) this._optLocEstimated_1.Checked = true;
         if(m_UGProj.LocEstimateMethod == 2) this._optLocEstimated_2.Checked = true;

         this.chkCreateOccupationsPage.Checked = m_UGProj.CreateOccupationsPage;
         this.chkIgnoreOccupations.Checked = m_UGProj.IgnoreOccupations;
         this.chkOccLocEqualsOccText.Checked = m_UGProj.OccLocEqualsOccText;

			//this.optUseCSS.Checked = m_UGProj.UseCSS;
         this.txtMainPageTitle.Text = m_UGProj.MainPageTitle;
         this.txtFirstFamily.Text = m_UGProj.FirstFamily.ToString();

         this.chkDoCompileHtml.Checked = m_UGProj.DoCompileHtml;
         this.txtHtmlHelpCompiler.Text = m_UGProj.HtmlHelpCompiler;

         this.chkExcludeIndvidualsBornAfter.Checked = m_UGProj.ExcludeLivingIndividualsAfterBirthCutOff;

         if(m_UGProj.BirthCutOffYears > this.nudBirthCutOffYears.Maximum) 
            m_UGProj.BirthCutOffYears = (int)this.nudBirthCutOffYears.Maximum;

         if(m_UGProj.BirthCutOffYears < this.nudBirthCutOffYears.Minimum)
            m_UGProj.BirthCutOffYears = (int)this.nudBirthCutOffYears.Minimum;

         this.nudBirthCutOffYears.Value = m_UGProj.BirthCutOffYears;

			if (m_UGProj.DaysRecent > this.nudDaysRecent.Maximum)
				m_UGProj.DaysRecent = (int)this.nudDaysRecent.Maximum;

			if (m_UGProj.DaysRecent < this.nudDaysRecent.Minimum)
				m_UGProj.DaysRecent = (int)this.nudDaysRecent.Minimum;

			this.nudDaysRecent.Value = m_UGProj.DaysRecent;
			
         this.chkDoDateEstimation.Checked = m_UGProj.DoDateEstimation;
         this.nudMaxLivingAge.Value = m_UGProj.MaxLivingAge;
         this.nudMinMarrAge.Value = m_UGProj.MinMarrAge;
         this.nudMinChildBirth.Value = m_UGProj.MinChildBirth;
         this.nudMaxChildBirth.Value = m_UGProj.MaxChildBirth;
         this.nudAbtEquals.Value = m_UGProj.AboutEquals;
         this.nudMaxAgeDif.Value = m_UGProj.MaxAgeDifference;
         this.nudMaxIterations.Value = m_UGProj.MaxIterations;

         //new option 
         this.chkAddSpaceToConc.Checked = m_UGProj.AddSpaceToConc;

         if (m_UGProj.PageFileExtension == 0)
            this.optPageExtensionHtml.Checked = true;
         else
            this.optPageExtensionHtm.Checked = true;

         if (m_UGProj.PageStyle == 0)
            this.optFamilyPages.Checked = true;
         else
            this.optPersonPages.Checked = true;


         this.txtMainPageText.Text = m_UGProj.MainPageText.Text;

         this.txtIndiIndexPageText.Text = m_UGProj.IndiIndexPageText.Text;
         this.txtStatsPageText.Text = m_UGProj.StatsPageText.Text;
         this.txtChronoPageText.Text = m_UGProj.ChronoPageText.Text;
         this.txtLocationsPageText.Text = m_UGProj.LocationsPageText.Text;
         this.txtOccupationsPageText.Text = m_UGProj.OccupationsPageText.Text;
         this.txtSurnamesPageText.Text = m_UGProj.SurnamesPageText.Text;

         this.txtIndiIndexPageTitle.Text = m_UGProj.IndiIndexPageTitle;
         this.txtStatsPageTitle.Text = m_UGProj.StatsPageTitle;
         this.txtChronoPageTitle.Text = m_UGProj.ChronoPageTitle;
         this.txtLocationsPageTitle.Text = m_UGProj.LocationsPageTitle;
         this.txtOccupationsPageTitle.Text = m_UGProj.OccupationsPageTitle;
         this.txtSurnamesPageTitle.Text = m_UGProj.SurnamesPageTitle;

         this.chkUseTagToText.Checked = m_UGProj.UseTagToText;

         this.txtMarkForMoreLinks.Text = m_UGProj.MarkForMoreLinks;

         this.nudMaxIndPerPage.Value = m_UGProj.MaxIndPerPage;

         this.txtPageTitleString.Text = m_UGProj.PersonPages.PageTitleString;

         this.txtBorn.Text = m_UGProj.PersonPages.BornString;
         this.txtDied.Text = m_UGProj.PersonPages.DiedString;
         this.txtMarried.Text = m_UGProj.PersonPages.MarriedString;
         this.txtFather.Text = m_UGProj.PersonPages.FatherString;
         this.txtMother.Text = m_UGProj.PersonPages.MotherString;
         this.txtFemale.Text = m_UGProj.PersonPages.FemaleString;
			this.txtMale.Text = m_UGProj.PersonPages.MaleString;
         this.txtFamily.Text = m_UGProj.PersonPages.FamilyString;
         this.txtSource.Text = m_UGProj.PersonPages.SourcesString;
         this.txtNotes.Text = m_UGProj.PersonPages.NotesString;
         this.txtMarriage.Text = m_UGProj.PersonPages.MarriageString;
         this.txtLastUpdated.Text = m_UGProj.PersonPages.LastUpdatedString;

         this.txtFrom.Text = m_UGProj.DateStrings.FROM;
         this.txtTO.Text = m_UGProj.DateStrings.TO;
         this.txtBEF.Text = m_UGProj.DateStrings.BEF;
         this.txtAFT.Text = m_UGProj.DateStrings.AFT;
         this.txtBET.Text = m_UGProj.DateStrings.BET;
         this.txtAND.Text = m_UGProj.DateStrings.AND;
         this.txtABT.Text = m_UGProj.DateStrings.ABT;
         this.txtEST.Text = m_UGProj.DateStrings.EST;
         this.txtCAL.Text = m_UGProj.DateStrings.CAL;
         this.txtINT.Text = m_UGProj.DateStrings.INT;

			this.chkDisplayMenuAtTop.Checked = m_UGProj.FamPageMenuAtTop;

			if (m_UGProj.UseCSS == true) m_UGProj.CSSOption = 1;

			if (m_UGProj.CSSOption == 0) this.optNoCSS.Checked = true;
			if (m_UGProj.CSSOption == 1) this.optUseCSS.Checked = true;
			if (m_UGProj.CSSOption == 2) this.optCopyCSS.Checked = true;

			this.txtCSS.Text = m_UGProj.CSSFile;

			if (m_UGProj.CSSFile.Trim().Length == 0 && m_UGProj.CSSOption == 2) this.optNoCSS.Checked = true;

			this.chkUseRSS.Checked = m_UGProj.CreateRssFeed;
			this.txtRssDescription.Text = m_UGProj.RssDescription;
			this.txtRssRootUrl.Text = m_UGProj.RssRootURL;

			this.chkDotlCreateDescendantOutline.Checked = m_UGProj.DotlCreateDescendantOutline;
			this.txtDotlInvdividuals.Text = m_UGProj.DotlIndividuals;
			if (m_UGProj.DotlNumGenerations < 1 || m_UGProj.DotlNumGenerations > 15)
				m_UGProj.DotlNumGenerations = 10;
			this.nudDotlNumGenerations.Value = m_UGProj.DotlNumGenerations;

			if (m_UGProj.DotlNumberingStyle == 0) this.optDotlNumberDAboville.Checked = true;
			if (m_UGProj.DotlNumberingStyle == 1) this.optDotlNumberHenry.Checked = true ;
			if (m_UGProj.DotlNumberingStyle == 2) this.optDotlNumberModifiedHenry.Checked = true;

      }

      /// <summary>
      /// Get the settings from the dialog box and update the configuration
      /// </summary>
      private void GetSettings()
      {
         m_UGProj.ProjectName = this.txtProjectName.Text.Trim();
         m_UGProj.GedComFile = this.txtGEDCOMFile.Text.Trim();
         m_UGProj.GedcomPath = this.txtGEDCOMPath.Text.Trim();
         m_UGProj.HtmlPath = this.txtHtmlPath.Text;

         m_UGProj.AuthorsName = this.txtAuthorName.Text.Trim();
         m_UGProj.AuthorsEmail = this.txtEmail.Text.Trim();
         m_UGProj.AuthorsUrl = this.txtAuthorsURL.Text.Trim();
			m_UGProj.HomeEqualsHomePage = this.chkHomeEqualsHomePage.Checked;
         m_UGProj.MainPageTitle = this.txtMainPageTitle.Text.Trim();
         m_UGProj.ChronoCutOff = (short)this.nudChronoCutoff.Value;
         m_UGProj.FamFilePrefix = this.txtFamFilePrefix.Text.ToLower();

         if (m_UGProj.FamFilePrefix.Length < 3)
            m_UGProj.FamFilePrefix = m_UGProj.FamFilePrefix + new string('_', 3 - m_UGProj.FamFilePrefix.Length);

         if (_optLocEstimated_0.Checked == true) m_UGProj.LocEstimateMethod = 0;
         if (_optLocEstimated_1.Checked == true) m_UGProj.LocEstimateMethod = 1;
         if (_optLocEstimated_2.Checked == true) m_UGProj.LocEstimateMethod = 2;

         m_UGProj.BackgroundFile = this.txtBackgroundImage.Text;
         m_UGProj.BannerFile = this.txtBannerImage.Text;

			m_UGProj.AddPedigreeToPerson = this.chkAddPedigreeToPerson.Checked;
         m_UGProj.CreateLocPage = this.chkCreateLocationsPage.Checked;
         m_UGProj.CreateOccupationsPage = this.chkCreateOccupationsPage.Checked;
         //m_UGProj.DisplayOptionBeforeBuild = this.chkDisplayOptionBeforeBuild.Check;
         m_UGProj.HiLiteMostRecent = this.chkHighlight.Checked;
         m_UGProj.IgnoreOccupations = this.chkIgnoreOccupations.Checked;
         m_UGProj.LocDisplayEventDates = this.chkLocDisplayEvents.Checked;
         m_UGProj.SourcesInNewWindow = this.chkSourceInNewWindow.Checked;
         m_UGProj.LocReverseOrder = this.chkLocReverseOrder.Checked;
         m_UGProj.MostRecentOnIndex = this.chkMostRecentOnIndex.Checked;
			m_UGProj.SuppressLastUpdateOnHomePage = this.chkSuppressLastUpdateOnHomePage.Checked;
         m_UGProj.MostRecentOnPage = this.chkMostRecentOnPage.Checked;
         m_UGProj.OccLocEqualsOccText = this.chkOccLocEqualsOccText.Checked;
         m_UGProj.AddUserCopyright = this.chkAddCopyright.Checked;
         m_UGProj.AddKeywords = this.chkAddKeywords.Checked;
         m_UGProj.ChronoLinks = this.chkChronoLinks.Checked;
         m_UGProj.CreateChronoPage = this.chkCreateChronoPage.Checked;
         this.nudChronoCutoff.Visible = this.chkCreateChronoPage.Checked;
         this.lblChronoCutoff.Visible = this.chkCreateChronoPage.Checked;

         m_UGProj.CreateGendexFile = this.chkCreateGendexFile.Checked;

         m_UGProj.CreateListReport = this.chkCreateListReport.Checked;

         m_UGProj.CreateStatsPage = this.chkCreateStatsPage.Checked;
         m_UGProj.DisplayMRIN = this.chkDisplayMRIN.Checked;
         m_UGProj.DisplayRIN = this.chkDisplayRIN.Checked;

         m_UGProj.ExcludeNotes = this.chkExcludeNotes.Checked;
         m_UGProj.AddSurnameCount = this.chkSurnameCount.Checked;
         m_UGProj.SurnamesAllCaps = this.chkSurnamesAllCaps.Checked;
			//m_UGProj.UseCSS = this.optUseCSS.Checked;
         //m_UGProj.WarnFileOverwrite = this.chkWarnFileOverwrite.Checked;
         //m_UGProj.WarnNonAnsi = this.chkWarnNonAnsi.Checked;
         m_UGProj.UseBackground = this.chkBackground.Checked;
         m_UGProj.UseBanner = this.chkBanner.Checked;
         m_UGProj.Privacy = this.chkPrivate.Checked;
         m_UGProj.FirstFamily = System.Int32.Parse(txtFirstFamily.Text);
         m_UGProj.DoCompileHtml = this.chkDoCompileHtml.Checked;
         m_UGProj.HtmlHelpCompiler = this.txtHtmlHelpCompiler.Text;

         m_UGProj.ExcludeLivingIndividualsAfterBirthCutOff = this.chkExcludeIndvidualsBornAfter.Checked;
         m_UGProj.BirthCutOffYears = (int)this.nudBirthCutOffYears.Value;

         m_UGProj.DoDateEstimation = this.chkDoDateEstimation.Checked;
         m_UGProj.MaxLivingAge = (int)this.nudMaxLivingAge.Value;
         m_UGProj.MinMarrAge = (int)this.nudMinMarrAge.Value;
         m_UGProj.MinChildBirth = (int)this.nudMinChildBirth.Value;
         m_UGProj.MaxChildBirth = (int)this.nudMaxChildBirth.Value;
         m_UGProj.AboutEquals = (int)this.nudAbtEquals.Value;
         m_UGProj.MaxAgeDifference = (int)this.nudMaxAgeDif.Value;
         m_UGProj.MaxIterations = (int)this.nudMaxIterations.Value;

			m_UGProj.DaysRecent = (int)this.nudDaysRecent.Value;

         //new options
         m_UGProj.AddSpaceToConc = this.chkAddSpaceToConc.Checked;

         if (this.optPageExtensionHtml.Checked == true)
            m_UGProj.PageFileExtension = 0;
         else
            m_UGProj.PageFileExtension = 1;

         if (this.optFamilyPages.Checked == true)
            m_UGProj.PageStyle = 0;
         else
            m_UGProj.PageStyle = 1;

         m_UGProj.MainPageText = new CDATA(this.txtMainPageText.Text);
         m_UGProj.IndiIndexPageText = new CDATA(this.txtIndiIndexPageText.Text);
         m_UGProj.StatsPageText = new CDATA(this.txtStatsPageText.Text);
         m_UGProj.ChronoPageText = new CDATA(this.txtChronoPageText.Text);
         m_UGProj.LocationsPageText = new CDATA(this.txtLocationsPageText.Text);
         m_UGProj.OccupationsPageText = new CDATA(this.txtOccupationsPageText.Text);
         m_UGProj.SurnamesPageText = new CDATA(this.txtSurnamesPageText.Text);

         m_UGProj.IndiIndexPageTitle = this.txtIndiIndexPageTitle.Text;
         m_UGProj.StatsPageTitle = this.txtStatsPageTitle.Text;
         m_UGProj.ChronoPageTitle = this.txtChronoPageTitle.Text;
         m_UGProj.LocationsPageTitle = this.txtLocationsPageTitle.Text;
         m_UGProj.OccupationsPageTitle = this.txtOccupationsPageTitle.Text;
         m_UGProj.SurnamesPageTitle = this.txtSurnamesPageTitle.Text;

         m_UGProj.UseTagToText = this.chkUseTagToText.Checked;
         m_UGProj.MarkForMoreLinks = this.txtMarkForMoreLinks.Text;

         m_UGProj.MaxIndPerPage = (int)this.nudMaxIndPerPage.Value;
         m_UGProj.PersonPages.PageTitleString = this.txtPageTitleString.Text;

         m_UGProj.PersonPages.BornString = this.txtBorn.Text;
         m_UGProj.PersonPages.DiedString = this.txtDied.Text;
         m_UGProj.PersonPages.MarriedString = this.txtMarried.Text;
         m_UGProj.PersonPages.FatherString = this.txtFather.Text;
         m_UGProj.PersonPages.MotherString = this.txtMother.Text;
         m_UGProj.PersonPages.FemaleString = this.txtFemale.Text;
			m_UGProj.PersonPages.MaleString = this.txtMale.Text;
         m_UGProj.PersonPages.FamilyString = this.txtFamily.Text;
         m_UGProj.PersonPages.SourcesString = this.txtSource.Text;
         m_UGProj.PersonPages.NotesString = this.txtNotes.Text;
         m_UGProj.PersonPages.MarriageString = this.txtMarriage.Text;
         m_UGProj.PersonPages.LastUpdatedString = this.txtLastUpdated.Text;

         m_UGProj.DateStrings.FROM = this.txtFrom.Text;
         m_UGProj.DateStrings.TO = this.txtTO.Text;
         m_UGProj.DateStrings.BEF = this.txtBEF.Text;
         m_UGProj.DateStrings.AFT = this.txtAFT.Text;
         m_UGProj.DateStrings.BET = this.txtBET.Text;
         m_UGProj.DateStrings.AND = this.txtAND.Text;
         m_UGProj.DateStrings.ABT = this.txtABT.Text;
         m_UGProj.DateStrings.EST = this.txtEST.Text;
         m_UGProj.DateStrings.CAL = this.txtCAL.Text;
         m_UGProj.DateStrings.INT = this.txtINT.Text;

			m_UGProj.FamPageMenuAtTop = this.chkDisplayMenuAtTop.Checked;

			if (this.optNoCSS.Checked == true) m_UGProj.CSSOption = 0;
			if (this.optUseCSS.Checked == true) m_UGProj.CSSOption = 1;
			if (this.optCopyCSS.Checked == true) m_UGProj.CSSOption = 2;

			m_UGProj.CSSFile = this.txtCSS.Text;

			m_UGProj.UseCSS = false; //now no longer used

			m_UGProj.CreateRssFeed = this.chkUseRSS.Checked;
			m_UGProj.RssDescription = this.txtRssDescription.Text;
			m_UGProj.RssRootURL = this.txtRssRootUrl.Text;

			m_UGProj.DotlCreateDescendantOutline = this.chkDotlCreateDescendantOutline.Checked;
			m_UGProj.DotlIndividuals = this.txtDotlInvdividuals.Text;
			m_UGProj.DotlNumGenerations = (int)this.nudDotlNumGenerations.Value;

			if (this.optDotlNumberDAboville.Checked) m_UGProj.DotlNumberingStyle = 0;
			if (this.optDotlNumberHenry.Checked) m_UGProj.DotlNumberingStyle = 1;
			if (this.optDotlNumberModifiedHenry.Checked) m_UGProj.DotlNumberingStyle = 2;

		}

      private string LongDirFix(string sIncomming, int iMax)
      {
         int I;
         int LblLen;
         //			int StringLen;
         string TempString;
         StringFunctions sf = new StringFunctions();

         TempString = sIncomming;
         LblLen = iMax;
         if (sf.Len(TempString) <= LblLen)
         {
            return TempString;
         }
         LblLen = LblLen - 6;
         for (I = sf.Len(TempString) - LblLen; I <= sf.Len(TempString); I++)
         {
            if (sf.Mid(TempString, I, 1) == "\\") break;
         } //
         return sf.Left(TempString, 3) + "..." + sf.Right(TempString, sf.Len(TempString) - (I - 1));
      }

      private void saveToolStripMenuItem_Click(object sender, EventArgs e)
      {
         m_UGProj.ProjectName = this.txtProjectName.Text;

         GetSettings();
         ConfigReader.SaveProject(m_ProjectFile, m_UGProj);
         m_DidSave = true;
      }

      private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
      {
         string path = FolderMethods.GetUserPath();

         // Set the properties on SaveFileDialog1 so the user is 
         // prompted to create the file if it doesn't exist 
         // or overwrite the file if it does exist.
         SaveFileDialog1.Title = "Save Project As...";
         SaveFileDialog1.CreatePrompt = true;
         SaveFileDialog1.OverwritePrompt = true;

         // Set the file name to myText.txt, set the type filter
         // to text files, and set the initial directory to drive C.
         SaveFileDialog1.FileName = Path.GetFileName(m_ProjectFile);
         SaveFileDialog1.DefaultExt = Path.GetExtension(m_ProjectFile);
         SaveFileDialog1.Filter = "Uncle GED Project Files (*.ugproj)|*.ugproj";
         SaveFileDialog1.InitialDirectory = path;

         // Call ShowDialog and check for a return value of DialogResult.OK,
         // which indicates that the file was saved. 
         DialogResult result = SaveFileDialog1.ShowDialog();
         

         if (result == DialogResult.OK)
         {
            GetSettings();
            m_ProjectFile = SaveFileDialog1.FileName;

            ConfigReader.SaveProject(m_ProjectFile, m_UGProj);

            m_DidSave = true;
         }

      }

      private void openMenuItem_Click(object sender, EventArgs e)
      {
         string path = FolderMethods.GetUserPath();

         openFileDialog1.FileName = Path.GetFileName(m_ProjectFile);
         openFileDialog1.InitialDirectory = path;
         openFileDialog1.Filter = "Uncle GED Project Files (*.ugproj)|*.ugproj";
         openFileDialog1.FilterIndex = 1;
         openFileDialog1.RestoreDirectory = false;

         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
            m_ProjectFile = openFileDialog1.FileName;
            m_UGProj = ConfigReader.LoadProject(m_ProjectFile);
            this.txtProjectName.Text = m_UGProj.ProjectName;
            DisplaySettings();
         }

         m_DidSave = true;
      }

      private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
      {
         DoShowHelp();
      }

      private void DoShowHelp()
      {
         Process myProcess = new Process();
         string path = FolderMethods.GetAppPath();

         try
         {
				System.Windows.Forms.Help.ShowHelp(this, path + "\\UncleGED.chm");

				//myProcess.StartInfo.FileName = path + "\\uged_ug.pdf";
				//myProcess.StartInfo.CreateNoWindow = true;
				//if (myProcess.Start() == false)
				//{
				//   MessageBox.Show("The Uncle GED user's guide (uged_ug.pdf) requires Adobe Acrobat Reader", "Adobe Acrobat Reader Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
				//}
         }
         catch (Win32Exception ex)
         {
            if (ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
            {
               MessageBox.Show(String.Format(ex.Message + "."));
            }

            else if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
            {
               MessageBox.Show(String.Format(ex.Message +
                  ". You do not have permission to view this file."));
            }
         }
         catch (Exception ex1)
         {
            MessageBox.Show(String.Format(ex1.Message));
         }
      }

      private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
      {
         AboutBox1 f = new AboutBox1();

         f.ShowDialog();

      }

      private void newMenuItem_Click(object sender, EventArgs e)
      {
         string path = FolderMethods.GetUserPath();

         SaveFileDialog1.Title = "Create New Project";
         SaveFileDialog1.CreatePrompt = true;
         SaveFileDialog1.OverwritePrompt = true;

         // Set the file name to myText.txt, set the type filter
         // to text files, and set the initial directory to drive C.
         SaveFileDialog1.FileName = "New Uncle Ged Project.ugproj";
         SaveFileDialog1.DefaultExt = "ugp";
         SaveFileDialog1.Filter = "Uncle GED Project Files (*.ugproj)|*.ugproj";
         SaveFileDialog1.InitialDirectory = path;

         // Call ShowDialog and check for a return value of DialogResult.OK,
         // which indicates that the file was saved. 
         DialogResult result = SaveFileDialog1.ShowDialog();


         if (result == DialogResult.OK)
         {
            m_UGProj = new UGedProject();

            DisplaySettings();

            m_ProjectFile = SaveFileDialog1.FileName;

            ConfigReader.SaveProject(m_ProjectFile, m_UGProj);
            m_DidSave = true;
         }

      }

      private void txtFirstFamily_TextChanged(object sender, EventArgs e)
      {
        if(!Microsoft.VisualBasic.Information.IsNumeric(txtFirstFamily.Text))
        {
           txtFirstFamily.Text = "0";
        }
      }

      private void btnSetHtmlCompilerPath_Click(object sender, EventArgs e)
      {
         this.folderBrowserDialog1.SelectedPath = m_UGProj.HtmlHelpCompiler;

         if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
         {
            m_UGProj.HtmlHelpCompiler = this.folderBrowserDialog1.SelectedPath;
            this.txtHtmlHelpCompiler.Text = m_UGProj.HtmlHelpCompiler;
         }
      }

      private void viewLogToolStripMenuItem_Click(object sender, EventArgs e)
      {
         DoViewLog();
      }

      private void btnSelectGedcom_Click(object sender, EventArgs e)
      {
         openFileDialog1.FileName = m_UGProj.GedComFile;
         openFileDialog1.InitialDirectory = m_UGProj.GedcomPath;
         openFileDialog1.Filter = "GEDCOM Files (*.ged)|*.ged";
         openFileDialog1.FilterIndex = 1;
         openFileDialog1.RestoreDirectory = false;

         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
            m_UGProj.GedComFile = Path.GetFileName(openFileDialog1.FileName);
            m_UGProj.GedcomPath = Path.GetDirectoryName(openFileDialog1.FileName);

            this.txtGEDCOMFile.Text = m_UGProj.GedComFile;
            this.txtGEDCOMPath.Text = m_UGProj.GedcomPath;
         }
      }

      private void btnSetHtmlPath_Click(object sender, EventArgs e)
      {
         this.folderBrowserDialog1.SelectedPath = m_UGProj.HtmlPath;

         if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
         {
            m_UGProj.HtmlPath = this.folderBrowserDialog1.SelectedPath;
            this.txtHtmlPath.Text = m_UGProj.HtmlPath;
         }
      }

      private void buildPagesToolStripMenuItem_Click(object sender, EventArgs e)
      {
         DoBuildPages();
      }

      private void DoBuildPages()
      {
         m_UGProj.ProjectName = this.txtProjectName.Text;

         GetSettings();
         ConfigReader.SaveProject(m_ProjectFile, m_UGProj);
         m_DidSave = true;

         BuildPages bp = new BuildPages(m_UGProj, m_localSettings, _logger);
         DialogResult dr = bp.ShowDialog();

         if (dr == DialogResult.Yes)
         {
            DoViewPages();
         }
      }

      private void btnBuildProject_Click(object sender, EventArgs e)
      {
         DoBuildPages();
      }

      private void toolStripButton1_Click(object sender, EventArgs e)
      {
         DoBuildPages();
      }

      private void DoViewPages()
      {
         Process myProcess = new Process();

         try
         {

            if (m_UGProj.PageFileExtension == 0)
               myProcess.StartInfo.FileName = m_UGProj.HtmlPath + "\\index.html";
            else
               myProcess.StartInfo.FileName = m_UGProj.HtmlPath + "\\index.htm";

            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
         }
         catch (Win32Exception ex)
         {
            if (ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
            {
               MessageBox.Show(String.Format(ex.Message + ". Check the HTML path setting."));
            }

            else if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
            {
               MessageBox.Show(String.Format(ex.Message +
                  ". You do not have permission to view this file."));
            }
         }
         catch (Exception ex1)
         {
            MessageBox.Show(String.Format(ex1.Message));
         }

      }

      private void viewPagesToolStripMenuItem_Click(object sender, EventArgs e)
      {
         DoViewPages();
      }

      private void toolStripButton2_Click(object sender, EventArgs e)
      {
         DoViewPages();
      }

      private void DoViewLog()
      {
         Process myProcess = new Process();

         try
         {
            string path = FolderMethods.GetAppPath();

            myProcess.StartInfo.FileName = path + "\\ugedlog.txt";

            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
         }
         catch (Win32Exception ex)
         {
            if (ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
            {
               MessageBox.Show(String.Format(ex.Message + ". Check the HTML path setting."));
            }

            else if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
            {
               MessageBox.Show(String.Format(ex.Message +
                  ". You do not have permission to view this file."));
            }
         }
         catch (Exception ex1)
         {
            MessageBox.Show(String.Format(ex1.Message));
         }

      }

      private void toolStripButton3_Click(object sender, EventArgs e)
      {
         DoShowHelp();
      }

      private void btnExit_Click(object sender, EventArgs e)
      {
			ExitUncleGed();
      }

		private void cancelToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ExitUncleGed();
		}

		private void ExitUncleGed()
		{
			m_UGProj.ProjectName = this.txtProjectName.Text;

			GetSettings();
			ConfigReader.SaveProject(m_ProjectFile, m_UGProj);

			string path = FolderMethods.GetUserPath();
			m_localSettings.MostRecentProject = m_ProjectFile;
			ConfigReader.SaveSettings(path + "\\projects.cfg", m_localSettings);

			m_DidSave = true;

			this.Close();
		}

      private void tagToTextToolStripMenuItem_Click(object sender, EventArgs e)
      {
         Process myProcess = new Process();

         try
         {
            string path = FolderMethods.GetUserPath();

            myProcess.StartInfo.FileName = path + "\\tagtotext.txt";

            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
         }
         catch (Win32Exception ex)
         {
            if (ex.NativeErrorCode == ERROR_FILE_NOT_FOUND)
            {
               MessageBox.Show(String.Format(ex.Message + ". Check the HTML path setting."));
            }

            else if (ex.NativeErrorCode == ERROR_ACCESS_DENIED)
            {
               MessageBox.Show(String.Format(ex.Message +
                  ". You do not have permission to view this file."));
            }
         }
         catch (Exception ex1)
         {
            MessageBox.Show(String.Format(ex1.Message));
         }
		}

		private void chkPrivate_CheckedChanged(object sender, EventArgs e)
		{
			if (chkPrivate.Checked == true)
			{
				chkExcludeIndvidualsBornAfter.Checked = false;
			}
		}

		private void chkExcludeIndvidualsBornAfter_CheckedChanged(object sender, EventArgs e)
		{
			if (chkExcludeIndvidualsBornAfter.Checked == true)
			{
				chkPrivate.Checked = false;
			}
		}

		private void btnGetCSSLoc_Click(object sender, EventArgs e)
		{
			string path = FolderMethods.GetUserPath();

			openFileDialog1.FileName = m_UGProj.CSSFile;
			openFileDialog1.InitialDirectory = path;
			openFileDialog1.Filter = "Cascading Style Sheets (*.css)|*.css";
			openFileDialog1.FilterIndex = 1;
			openFileDialog1.RestoreDirectory = false;

			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				m_UGProj.CSSFile = openFileDialog1.FileName;
				this.txtCSS.Text = m_UGProj.CSSFile;
			}
		}

		private void tmrStartup_Tick(object sender, EventArgs e)
		{
			tmrStartup.Stop();

			try
			{
				if (m_localSettings.DidCopyDefaultFiles == false)
				{
					string uPath = FolderMethods.GetUserPath();
					this.lblFileCopyStatus.Text = "First time copy of default files to " + uPath;
					ConfigReader.CopyDefaultFiles();
					m_localSettings.DidCopyDefaultFiles = true;
				}

			}
			catch (Exception ex)
			{
				_logger.ErrorFormat("Error copying default files: {0}", ex.Message);
			}

			this.lblFileCopyStatus.Text = "";
		}

   }
}